﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.IO;
using System.ComponentModel;
using System.Windows.Input;
using System.Windows;
using StoryMaker.Data;

namespace StoryMaker.Common
{
    public class OpenStoryViewModel : NotifyPropertyChangedBase
    {
        public OpenStoryViewModel()
        {
            Stories = new ObservableCollection<StoryModel>();
            loadData();
        }

        private void loadData()
        {
            foreach (string path in Directory.GetFiles(FolderPath))
            {
                BackgroundWorker backgroundWorker = new BackgroundWorker();
                backgroundWorker.WorkerSupportsCancellation = true;
                backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
                backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
                backgroundWorker.RunWorkerAsync(path);
            }
        }

        void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Stories.Add((StoryModel)e.Result);
        }

        void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            XMLDataHandler<StoryModel> dataHandler = new XMLDataHandler<StoryModel>((string)e.Argument);
            e.Result = dataHandler.LoadData();
        }

        private StoryModel _selectedStory;
        public StoryModel SelectedStory
        {
            get { return _selectedStory; }
            set
            {
                if (!EqualityComparer<StoryModel>.Default.Equals(_selectedStory, value))
                {
                    _selectedStory = value;
                    NotifyPropertyChanged("SelectedStory");
                }
            }
        }

        private GenericCommand _cancelCommand;
        public ICommand CancelCommand
        {
            get
            {
                if (_cancelCommand == null)
                {
                    //_cancelCommand = new GenericCommand(() => 
                }
                return _cancelCommand;
            }
        }

        private GenericCommand _okCommand;
        public ICommand OKCommand
        {
            get
            {
                if (_okCommand == null)
                {
                    //_okCommand = new GenericCommand(() => ApplicationCommands.Close.Execute(null, null));
                }
                return _okCommand;
            }
        }

        private ObservableCollection<StoryModel> _stories;
        public ObservableCollection<StoryModel> Stories
        {
            get { return _stories; }
            set
            {
                if (!EqualityComparer<ObservableCollection<StoryModel>>.Default.Equals(_stories, value))
                {
                    _stories = value;
                    NotifyPropertyChanged("Stories");
                }
            }
        }

        public static string FolderPath
        {
            get
            {
                string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Story Maker");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                return path;
            }
        }
    }
}
